package com.itheima.dao;

import com.itheima.pojo.News;
import com.itheima.producer.KafkaSpiderProducer;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

import java.beans.PropertyVetoException;

/**
 * 用来交互数据库
 */
public class NewsDao extends JdbcTemplate {


    //数据源
    private static ComboPooledDataSource dataSource;
    //定义信息
    static {
        dataSource = new ComboPooledDataSource();
        try {
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://192.168.72.141:3306/gossip?characterEncoding=utf-8");
            dataSource.setUser("root");
            dataSource.setPassword("123");

        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }

    public NewsDao(){
        //将初始化的数据源赋值给父类的DataSource
        //然后才能正常使用父类所有的方法
        super(dataSource);
    }
    /**
     * 保存新闻对象到数据库
     */
    public void saveNews(News news){
        String sql = "insert into news values(?,?,?,?,?,?,?)";
        this.update(sql,news.getId(),news.getTitle(),news.getDocurl(),news.getContent(),news.getEditor(),news.getTime(),news.getSource());
    }

}
